package gdut.jmxywz.action;

import gdut.jmxywz.been.ClassDown;
import gdut.jmxywz.been.Major;
import gdut.jmxywz.been.Stclass;
import gdut.jmxywz.been.Student;
import gdut.jmxywz.util.PageModel;

import java.io.File;
import java.io.FileInputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import org.apache.struts2.ServletActionContext;

public class majorAction extends BaseAction {
	
	private Major major;
	
	private int majorId;
	
/*----------------分页----------------------*/
	private PageModel pageModel;
	
	private int currentPage=1;
	
	private int pageSize=5;
	
/*----------------检索----------------------*/
	private String searchSelect="";
	
	private String like="like";
	
	private String searchValue="";
	
	private String excelParameter="";
	
/*----------------Excel导入导出----------------------*/
	private File excelFile;
	
	private boolean checkbox;

/***********************geter和setter方法*****************************/	
	public Major getMajor() {
		return major;
	}
	
	public void setMajor(Major major) {
		this.major = major;
	}
	
	public PageModel getPageModel() {
		return pageModel;
	}

	public void setPageModel(PageModel pageModel) {
		this.pageModel = pageModel;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	
	public String getSearchSelect() {
		return searchSelect;
	}

	public void setSearchSelect(String searchSelect) {
		this.searchSelect = searchSelect;
	}

	public String getLike() {
		return like;
	}

	public void setLike(String like) {
		this.like = like;
	}

	public String getSearchValue() {
		return searchValue;
	}

	public void setSearchValue(String searchValue)throws UnsupportedEncodingException {
		this.searchValue = new String(searchValue.getBytes("ISO-8859-1"),"utf-8");
	}

	public String getExcelParameter() {
		return excelParameter;
	}

	public void setExcelParameter(String excelParameter) {
		this.excelParameter = excelParameter;
	}
	
	public int getMajorId() {
		return majorId;
	}

	public void setMajorId(int majorId) {
		this.majorId = majorId;
	}
	
	public File getExcelFile() {
		return excelFile;
	}

	public void setExcelFile(File excelFile) {
		this.excelFile = excelFile;
	}

	public boolean isCheckbox() {
		return checkbox;
	}

	public void setCheckbox(boolean checkbox) {
		this.checkbox = checkbox;
	}

	/*################################################################################*/
	public String Add() throws Exception {
		// TODO Auto-generated method stub
		
		boolean bl=this.getCommonService().saveObject(major);
		if(bl)
			return "add";
		else
			return "error";
	}


	public String Show() throws Exception {
		// TODO Auto-generated method stub
		String hql="from Major";
		pageModel=this.getCommonService().findPage(hql, null, currentPage, pageSize, "majorShow.action", "");
		
		return "show";
	}

	
	public String Search() throws Exception {
		// TODO Auto-generated method stub
		String hql="from Major";
		String searchParameter="";
		
		String searchValue1=searchValue;
		
		if (like.equals("like")) {
			hql = hql + " as bf  where bf." + searchSelect + " like '%"+ searchValue1 + "%'";
		} else {
			hql = hql + " as bf  where bf." + searchSelect + " ='"+ searchValue1 + "'";
		}
		
		String enSearchValue=java.net.URLEncoder.encode(searchValue1,"UTF-8");//URL编码转换
		excelParameter="?searchSelect="+searchSelect+"&searchValue="+enSearchValue+"&like="+like;
		
		searchParameter="&searchSelect="+searchSelect+"&searchValue="+enSearchValue+"&like="+like;
		
		pageModel=this.getCommonService().findPage(hql, null, currentPage, pageSize,"majorSearch.action",searchParameter);

		return "search";
	}

	
	public String Detail() throws Exception {
		// TODO Auto-generated method stub
		major=(Major)this.getCommonService().findByIntId(majorId, "gdut.jmxywz.been.Major");
		if(major==null){
			return "error";
		}
		return "detail";
	}

	
	public String PreUpdate() throws Exception {
		// TODO Auto-generated method stub
		major=(Major)this.getCommonService().findByIntId(majorId, "gdut.jmxywz.been.Major");
		if(major==null){
			return "error";
		}
		return "preUpdate";
	}

	
	public String Update() throws Exception {
		// TODO Auto-generated method stub
		boolean bl=this.getCommonService().updateObject(major);
		if(!bl){
			return "error";
		}
		return "update";
	}

	
	public String Delete() throws Exception {
		// TODO Auto-generated method stub
		/***
		 * 删除专业之前必须先删除班级，但删除班级前又必须先删除学生和资源下载关联
		 */
		/*删除班级记录之前，先删除关联学生*/
		String deleteList="select stu from Student stu where stu.stclass.major.majorId="+majorId;
		List deleteStudentList=this.getCommonService().findListByHql(deleteList, null);
		for(int i=0;i<deleteStudentList.size();i++){
			boolean d=this.getCommonService().deleteObject((Student)deleteStudentList.get(i));
			if(!d){
				return "error";
			}
		}
		
		/*删除班级记录之前，先删除课件下载关联*/
		String deleteList2="select cd from ClassDown cd where cd.stclass.major.majorId="+majorId;
		List deleteClassDownList=this.getCommonService().findListByHql(deleteList2, null);
		for(int i=0;i<deleteClassDownList.size();i++){
			boolean d=this.getCommonService().deleteObject((ClassDown)deleteClassDownList.get(i));
			if(!d){
				return "error";
			}
		}
		
		/*删除班级记录之前，先删除课件下载关联*/
		String deleteList3="select stc from Stclass stc where stc.major.majorId="+majorId;
		List deleteStclassList=this.getCommonService().findListByHql(deleteList3, null);
		for(int i=0;i<deleteStclassList.size();i++){
			boolean d=this.getCommonService().deleteObject((Stclass)deleteStclassList.get(i));
			if(!d){
				return "error";
			}
		}
		/*删除专业*/
		major=(Major)this.getCommonService().findByIntId(majorId, "gdut.jmxywz.been.Major");
		if(major==null){
			return "error";
		}
		
		boolean bl=this.getCommonService().deleteObject(major);
		if(!bl){
			return "error";
		}
		return "delete";
	}
	
	public String ImportExcel() throws Exception {
		// TODO Auto-generated method stub
		int count=0;
	
		if(excelFile==null)return "error";
		try{
			FileInputStream fis=new FileInputStream(excelFile);
	        jxl.Workbook jwb=Workbook.getWorkbook(fis);
	        int sheets=jwb.getNumberOfSheets();
	        for(int i=0;i<sheets;i++){
	        	Sheet rs=jwb.getSheet(i);
	        	int columnNum=rs.getColumns();
	        	int rowNum=rs.getRows();
	        	if(rowNum>0&&columnNum!=3){
	        		System.out.println("---------------导入格式不对-----------------"+rowNum);
	        		return "error";
	        	}
	        	
	        	Cell cell;
	        	
	        	for(int row=1;row<rowNum;row++){
	        		
	        		cell=rs.getCell(0, row);
	        		System.out.println("--------------------------------"+cell.getContents());
	        		String hql="from Major m where m.majorName='"+cell.getContents()+"'";
	        		List l=this.getCommonService().findListByHql(hql, null);
	        		
	        		if(l.size()==0){
	        			major=new Major();
	        			major.setMajorName(cell.getContents());
	        		}else{
	        			major=(Major)l.get(0);
	        		}
	        		
	        		cell=rs.getCell(1, row);
	        		major.setMajorMaster(cell.getContents());
	        		cell=rs.getCell(2, row);
	        		major.setMajorDescript(cell.getContents());
	        		
	        		boolean bl;
	        		if(l.size()==0){
	        			bl=this.getCommonService().saveObject(major);
	        		}else{
	        			
	        			bl=this.getCommonService().updateObject(major);
	        		}
	        		if(!bl){
	        			System.out.println("=================存入数据库出错=================");
	        		}
	        		count++;
	        	}
	        	
	        }
	        System.out.println("=================一共导入数据数：================="+count);
	       
	        jwb.close();    	
	        fis.close();
			}catch(Exception e){
				e.printStackTrace();
				
			}
		return "importExcel";
	}

	
	public void ExportExcel() throws Exception {
		// TODO Auto-generated method stub
		try{
			String[] str=new String[]{"专业名称","专业主任","专业描述"};
			List list;
			
			if(!searchSelect.equals("")){
				String hql="from Major";
				System.out.println("++++++++++++++++++++++++++++"+searchSelect+like+searchValue);
				String searchValue1=searchValue;
				
				if (like.equals("like")) {
					hql = hql + " as bf  where bf." + searchSelect + " like '%"+ searchValue1 + "%'";
				} else {
					hql = hql + " as bf  where bf." + searchSelect + " ='"+ searchValue1 + "'";
				}
				
				list=this.getCommonService().findListByHql(hql, null);
			}else{
				String hql="from Major";
				list=this.getCommonService().findListByHql(hql, null);
			
			}
			if(list==null)
				return;
			HttpServletResponse response=ServletActionContext.getResponse();
			response.reset();
			response.setContentType("application/vnd.ms-excel");
			response.setContentType("APPLICATION/OCTET-STREAM ");
			response.setHeader("Content-Disposition","attachment; filename="+java.net.URLEncoder.encode("专业.xls","UTF-8"));
			WritableWorkbook wwb=Workbook.createWorkbook(response.getOutputStream());
			WritableSheet sheet=wwb.createSheet("sheet1", 0);
			sheet.setColumnView(0, 20);
			sheet.setColumnView(1, 20);
			sheet.setColumnView(3, 20);
			
			Label label=null;
			for(int i=0;i<str.length;i++){
				label=new Label(i,0,str[i]);
				sheet.addCell(label);
			}
			for(int i=0;i<list.size();i++){
				major=(Major)list.get(i);
				
				label=new Label(0,i+1,major.getMajorName());
				sheet.addCell(label);
				label=new Label(1,i+1,major.getMajorMaster());
				sheet.addCell(label);
				label=new Label(2,i+1,major.getMajorDescript());
				sheet.addCell(label);
		
			}
			wwb.write();
			wwb.close();
			
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}

	
}
